﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Host.RemoteHandler;
using DWF.Studio.Models;
using DWF.Studio.RemoteCtrls;
using DWF.Studio.RemoteCtrls.Proxy;
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DWF.Studio.Host
{
    internal class RemoteSnippetExplorerHost : IDisposable
    {
        /// <summary>
        /// 远程片段栏代理
        /// </summary>
        private SnippetExplorerProxy _ctrlSnippetProxy = null;

        /// <summary>
        /// 片段栏
        /// </summary>
        private FrameworkElement _snippetExplorer = null;

        /// <summary>
        /// 片段变更处理
        /// </summary>
        private GenericHandler<ShowSnippetEventArgs> _showSnippetHandler = null;

        /// <summary>
        /// 片段依赖活动校验
        /// </summary>
        private GenericHandler<RepairSnippetEventArgs> _repairSnippetHandler = null;
        /// <summary>
        /// 片段依赖活动校验
        /// </summary>
        private GenericHandler<EventArgs> _checkSnippetHandler = null;
        /// <summary>
        /// 片段重复提示
        /// </summary>
        private GenericHandler<SnippetCoverEventArgs> _coverSnippetHandler = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="snippetExplorer"></param>
        /// <param name="snippetAssemblies"></param>
        public RemoteSnippetExplorerHost(SnippetExplorerProxy snippetExplorer)
        {
            _ctrlSnippetProxy = snippetExplorer;

            _showSnippetHandler = new GenericHandler<ShowSnippetEventArgs>();
            _showSnippetHandler.SetAction(showSnippet);
            this._ctrlSnippetProxy.SnippetShow += _showSnippetHandler.Action;

            _repairSnippetHandler = new GenericHandler<RepairSnippetEventArgs>();
            _repairSnippetHandler.SetAction(repairSnippet);
            this._ctrlSnippetProxy.RepairSnippetActivity += _repairSnippetHandler.Action;

            _checkSnippetHandler = new GenericHandler<EventArgs>();
            _checkSnippetHandler.SetAction(checkSnippet);
            this._ctrlSnippetProxy.CheckSnippetActivity += _checkSnippetHandler.Action;

            _coverSnippetHandler = new GenericHandler<SnippetCoverEventArgs>();
            _coverSnippetHandler.SetAction(isCoverSnippet);
            this._ctrlSnippetProxy.IsCoverSnippet += _coverSnippetHandler.Action;
        }

        /// <summary>
        /// 释放片段栏
        /// </summary>
        public void Dispose()
        {
            _ctrlSnippetProxy?.Dispose();
            _showSnippetHandler?.Dispose();
            _showSnippetHandler = null;
            _checkSnippetHandler?.Dispose();
            _checkSnippetHandler = null;
            this._ctrlSnippetProxy = null;
        }
        /// <summary>
        /// 获取片段栏
        /// </summary>
        /// <returns></returns>
        public FrameworkElement GetSnippetExplorer()
        {
            if (_snippetExplorer == null)
            {
                this._snippetExplorer = FrameworkElementAdapters.ContractToViewAdapter(_ctrlSnippetProxy.GetSnippetExplorer());
            }
            return _snippetExplorer;
        }
        //重新加载
        public void Reload()
        {
            _ctrlSnippetProxy.Reload();
        }
        /// <summary>
        /// 打开片段
        /// </summary>
        /// <returns></returns>
        private void showSnippet(ShowSnippetEventArgs e)
        {
            SnippetFile file = new SnippetFile()
            {
                Name = Path.GetFileName(e.SnippetName),
                FullName = e.SnippetName,
            };

            GlobalActionHandler.SnippetScriptShow?.Invoke(new TagEventArgs<SnippetFile>(file));
        }
        /// <summary>
        /// 修复片段依赖活动
        /// </summary>
        /// <returns></returns>
        private void repairSnippet(RepairSnippetEventArgs e)
        {
            GlobalActionHandler.RepairSnippetActivity?.Invoke(new TagEventArgs<string>(e.SnippetName));
        }
        private void checkSnippet(EventArgs e)
        {
            GlobalActionHandler.CheckSnippetActivity?.Invoke(EventArgs.Empty);
        }
        //选择是否覆盖片段
        private void isCoverSnippet(SnippetCoverEventArgs e)
        {
            GlobalActionHandler.IsCoverSnippet?.Invoke(new TagEventArgs<string, string, string>(e.SnippetName, e.FullName, e.TargetFolder));
        }
        //覆盖片段
        public void coverSnippet(string fullName, string targetFolder)
        {
            _ctrlSnippetProxy.CoverSnippet(fullName, targetFolder);
        }
    }
}
